#include "field.h"
#include <math.h>
#include <QPointF>
#include <QDebug>

Field::Field() :
    QGraphicsScene()
{
    setSceneRect(QRectF(0, 0, fieldWidth, fieldWidth));

    for(int i = 0; i < placeNumber; ++i){
        for(int j = 0; j < placeNumber; ++j){
            if(!(i < 3 && (j < 3 || j > 5) ||
               i > 5 && (j < 3 || j > 5)
              )) {

                int x = (i+1)*placeStep + i*placeDiam;
                int y = (j+1)*placeStep + j*placeDiam;
                Pit *pit = new Pit(x, y, j, i, placeDiam);
                pits.push_back(pit);
                addItem(pit);

                if(i != 4 || j != 4) {
                    Ball *ball = new Ball(x, y, j, i, placeDiam, this);
                    balls.push_back(ball);
                    addItem(ball);
                }
            }
        }
    }
}


Pit* Field::getPitByIndex(int row, int col){
    if(row >= 3 && row <= 5){
        if(col >= 0 && col <= 2){
            row %= 3;
            return pits[col*3 + row];
        }
        if(col >= 6 && col <= placeNumber){
            row %= 3;
            col %= 6;
            return pits[36 + col*3 + row];
        }
    }
    if(row >= 0 && row < placeNumber && col >= 3 && col <=5){
        col %= 3;
        return pits[9 + col*placeNumber + row];
    }
    return 0;
}

int Field::width() const
{
    return fieldWidth;
}
